package com.shelpe.service.simbaproxy.configuration;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shelpe.service.simbaproxy.client.AuthClient;
import com.shelpe.service.simbaproxy.dev.CompositeTBFilter;
import com.shelpe.service.simbaproxy.dev.TaobaoSandboxProperties;
import com.shelpe.service.simbaproxy.filter.ContentTypeFilter;
import com.shelpe.service.simbaproxy.filter.ErrorFilter;
import com.shelpe.service.simbaproxy.taobao.APIParameterBuilderFilter;
import com.shelpe.service.simbaproxy.taobao.PreValidateFilter;
import com.shelpe.service.simbaproxy.taobao.TaoBaoAPIProperties;
import com.shelpe.service.simbaproxy.taobao.TaobaoAPIService;
import com.shelpe.service.simbaproxy.taobao.TaobaoAPIServiceImpl;

@Configuration
@Profile({"dev", "local"})
@EnableConfigurationProperties(TaobaoSandboxProperties.class)
public class DevConfiguration {
	
	@Bean
	public TaobaoAPIService taobaoService(TaoBaoAPIProperties props){
		TaobaoAPIServiceImpl service = new TaobaoAPIServiceImpl();
		service.setProp(props);
		return service;
	}
	
	@Bean
	public TaobaoAPIService sandboxService(TaobaoSandboxProperties props){
		TaobaoAPIServiceImpl service = new TaobaoAPIServiceImpl();
		service.setProp(props);
		return service;
	}
	
	@Bean
	public CompositeTBFilter taobaoParamsFilter(@Qualifier("taobaoService") TaobaoAPIService taobaoAPIService, 
			@Qualifier("sandboxService") TaobaoAPIService sandboxAPIService, 
			AuthClient authService, ObjectMapper objectMapper){
		APIParameterBuilderFilter proxy =  new APIParameterBuilderFilter();
		proxy.setAuthClient(authService);
		proxy.setTaobaoAPIService(taobaoAPIService);
		proxy.setObjectMapper(objectMapper);
		
		APIParameterBuilderFilter sandbox =  new APIParameterBuilderFilter();
		sandbox.setAuthClient(authService);
		sandbox.setTaobaoAPIService(sandboxAPIService);
		sandbox.setObjectMapper(objectMapper);
		
		CompositeTBFilter filter = new CompositeTBFilter();
		filter.setProxy(proxy);
		filter.setSandboxProxy(sandbox);
		return filter;
	}
	@Bean
	public ContentTypeFilter contentTypeFilter(){
		return new ContentTypeFilter();
	}
	@Bean
	public ErrorFilter errorFilter(){
		return new ErrorFilter();
	}
	@Bean
	public PreValidateFilter preValidateFilter(){
		return new PreValidateFilter();
	}
}
